var form = layui.form;
var chenping = {
    /**
     * layui表单监听
     * @param elm 监听元素
     */
    formSubmit:function (elm='*') {
        form.on("submit("+elm+")",function (data) {
            chenping.request({
                url:$(data.form).attr('action'),
                type:$(data.form).attr('method'),
                data:data.field,
                success:function (res) {
                    if(res.code==1){
                        location.href = ''
                    }
                }
            })
            return false;
        })
    },

    /**
     * 服务请求
     * @param option 请求参数选项
     */
    request:function (option) {
        var opt = {
            url: option.url ? option.url : '',
            dataType: option.dataType ? option.dataType : 'json',
            data: option.data ? option.data : '',
            type: option.type ? option.type : 'post',
            success: function(succ) {
                chenping.tips(succ.msg, succ.code);
                if(typeof option.success =='function'){
                    option.success(succ)
                }
            },
            error: function(error) {
                var msg = "网络请求失败了请稍后再试";
                switch (error.status) {
                    case 404:
                        msg = "你所请求的地址不存在";
                        break;
                }
                chenping.tips(msg, error.status);
                if(typeof option.error =='function'){
                    option.error(error)
                }
            }
        }
        //网络请求
        $.ajax(opt)
    },

    /**
     * 提示
     * @param content 提示内容
     * @param status 状态
     * @param end 销毁后操作
     */
    tips: function(content, status, end) {
        let opt = {
            icon: 0
        }
        if (status == 1 || status == 200) {
            opt.icon = 1
        }
        if (status == 0 || status == 400) {
            opt.icon = 0
        }
        layer.msg(content, opt, end)
    },

    /**
     * 获取验证码
     * @param url 获取验证码的地址
     * @param title 提示标题
     * @param data 请求所需要发送到服务的数据
     */
    getSmsCode:function (url,title,data,success) {
        var smsCodeIndex = layer.load(1,{
            title:title,
            success:function () {
                chenping.request({
                    url:url,
                    data:data,
                    success:function (res) {
                        if(res.code==1){
                            layer.close(smsCodeIndex)
                        }else if(res.code==0) {
                            setTimeout(function () {
                                layer.close(smsCodeIndex)
                            },1500)
                        }
                        if(typeof success=='function'){
                            success(res);
                        }
                    },
                    error:function (err) {
                        layer.msg("网络错误请稍后再试！",{
                            end:function () {
                                layer.close(smsCodeIndex)
                            }
                        })
                    }
                })
            }
        })
    },

    /**
     * 系统退功能
     * @param url 请求服务退出接口
     */
    out:function (url) {
        var outindex = layer.load(2,{
            success:function () {
                chenping.request({
                    url:url,
                    success:function (res) {
                        if(res.code==1){
                            location.href = "/"
                        }else{
                            layer.close(outindex)
                        }
                    },
                    error:function (error) {
                        layer.close(outindex)
                    }
                })
            }
        })
    },

    /**
     * 验证倒计时获取
     * @param button 按钮
     * @param seconds 描述
     */
    countdown:function (button,seconds,param) {
        var text = $(button).text();
        chenping.getSmsCode(param.url,param.title,param.data,function (res) {
            if(res.code==1){
                var timer = setInterval(function () {
                    seconds--;
                    if (seconds <=0) {
                        $(button).attr("disabled",false).css({
                            "background-color":"#1E9FFF"
                        }).text(text);
                        clearInterval(timer);
                    }else{
                        $(button).css({
                            "background-color":"#707274"
                        }).attr("disabled",true).text(seconds+"秒后获取");
                    }
                }, 1000);
            }
        });
    },

    /**
     * 文件上传
     * @param param
     */
    upload:function (param) {
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: param.elm
            ,url: param.url
            ,done: function(res){
                chenping.tips(res.msg,res.code,function () {
                    if(typeof param.done=='function'){
                        param.done(res);
                    }
                })
            }
            ,error: function(){
                chenping.tips("网络请求错误，请稍后再试！！",0)
            }
        });
    }

}

